package com.henu.col05023.common.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.lob.LobHandler;

public class BaseDAO {
	private DataSource dataSource = null;

	private JdbcTemplate jdbcTemplate = null;

	private LobHandler lobHandler;

	public LobHandler getLobHandler() {
		return lobHandler;
	}

	public void setLobHandler(LobHandler lobHandler) {
		this.lobHandler = lobHandler;
	}

	public void setJdbcTemplate(DataSource dataSource) {
		jdbcTemplate = new JdbcTemplate(dataSource);
	}

	public JdbcTemplate getJdbcTemplate() {
		return this.jdbcTemplate;
	}

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;

		setJdbcTemplate(this.dataSource);
	}

	public DataSource getDataSource() {
		return this.dataSource;
	}

	public Connection getConnection() throws SQLException {
		Connection connection = this.dataSource.getConnection();
		return connection;
	}

	public Object queryForInt(String sql, List args) {
		Object obj = null;
		jdbcTemplate.queryForInt(
		    sql, args.toArray());
		return obj;
	}

	public void close(Connection connection) {
		try {
			connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public void close(Connection connection, Statement statement) {
		try {
			statement.close();
			connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public void close(Connection connection, PreparedStatement PStatement) {
		try {
			PStatement.close();
			connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public void close(Connection connection, Statement statement, ResultSet rest) {
		try {
			statement.close();
			connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
